package com.data.modules.smzh.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data.common.api.vo.Result;
import com.data.common.util.JwtIOTAuthUtil;
import com.data.modules.smzh.entity.SmzhScreenMsg;
import com.data.modules.smzh.mapper.SmzhScreenMsgMapper;
import com.data.modules.smzh.service.IFacedeviceService;
import com.data.modules.smzh.service.ISmzhScreenMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 智慧屏消息表
 * @Author: data-boot
 * @Date:   2023-01-06
 * @Version: V1.0
 */
@Service
public class SmzhScreenMsgServiceImpl extends ServiceImpl<SmzhScreenMsgMapper, SmzhScreenMsg> implements ISmzhScreenMsgService {

    @Autowired
    private SmzhScreenMsgMapper smzhScreenMsgMapper;

    @Autowired
    private IFacedeviceService facedeviceService;
    @Override
    public List<String> queryBuildNoByProjectId(List<String> projectlist) {
        return smzhScreenMsgMapper.searchBuildNoByProjectId(projectlist);
    }

    @Override
    public Result<?> smAddNoticInfo(SmzhScreenMsg smComAnnouncement, HttpServletRequest req) {
        try {
            String appId = JwtIOTAuthUtil.getTokenByRequest(req);
            String reaAppId = "";
            if(ObjectUtil.isEmpty(appId)){
                return Result.error(10001,"无效token");
            }else{
                reaAppId = facedeviceService.queryDevelAppIdByToken(appId);

                String userName = JwtIOTAuthUtil.getUserNameByRequest(req);

                smComAnnouncement.setRemakr(smComAnnouncement.getRemakr().replaceAll("https://www.sm-ioe.com/","../"));
                smComAnnouncement.setContent(reaAppId);
                smComAnnouncement.setCreateBy(userName);

                this.save(smComAnnouncement);

            }

        } catch (Exception e) {
            return Result.error(10001,e.getMessage());
        }
        return Result.ok(0,"操作成功");
    }

    @Override
    public Result<?> smDeleteNoticInfo(SmzhScreenMsg smComAnnouncement, HttpServletRequest req) {
        try {
            String appId = JwtIOTAuthUtil.getTokenByRequest(req);
            String reaAppId = "";
            if(ObjectUtil.isEmpty(appId)){
                return Result.error(10001,"无效token");
            }else{
                reaAppId = facedeviceService.queryDevelAppIdByToken(appId);

                this.removeById(smComAnnouncement.getId());
            }

        } catch (Exception e) {
            return Result.error(10001,e.getMessage());
        }
        return Result.ok(0,"操作成功");
    }

    @Override
    public Result<?> smQueryNoticeInfo(SmzhScreenMsg smComAnnouncement, HttpServletRequest req) {
        try {
            String appId = JwtIOTAuthUtil.getTokenByRequest(req);
            String reaAppId = "";
            if(ObjectUtil.isEmpty(appId)){
                return Result.error(10001,"无效token");
            }else{
                reaAppId = facedeviceService.queryDevelAppIdByToken(appId);

                SmzhScreenMsg vo = this.getById(smComAnnouncement.getId());
                if(ObjectUtil.isNotEmpty(vo.getRemakr())){
                    vo.setRemakr(vo.getRemakr().replace("../group1","https://www.sm-ioe.com/group1"));
                }

                return Result.ok(0,vo);
            }

        } catch (Exception e) {
            return Result.error(10001,e.getMessage());
        }
    }

    @Override
    public Result<?> searchNoticeListInfo(SmzhScreenMsg smComAnnouncement, HttpServletRequest req) {

        List<SmzhScreenMsg> list = new ArrayList<>();
        try {
            String appId = JwtIOTAuthUtil.getTokenByRequest(req);
            String reaAppId = "";
            if(ObjectUtil.isEmpty(appId)){
                return Result.error(10001,"无效token");
            }else{
                reaAppId = facedeviceService.queryDevelAppIdByToken(appId);

                list = this.lambdaQuery().eq(SmzhScreenMsg::getContent,reaAppId)
                        .eq(ObjectUtil.isNotEmpty(smComAnnouncement.getProjectNo()),SmzhScreenMsg::getProjectNo,smComAnnouncement.getProjectNo())
                        .list();

                if(ObjectUtil.isNotEmpty(list)){

                    list.stream().forEach(x->{
                        if(ObjectUtil.isNotEmpty(x.getRemakr())){
                            x.setRemakr(x.getRemakr().replace("../group1","https://www.sm-ioe.com/group1"));
                        }
                    });
                }
            }

        } catch (Exception e) {
            return Result.error(10001,e.getMessage());
        }
        return Result.ok(0,list);
    }

    @Override
    public Result<?> smModityNoticInfo(SmzhScreenMsg smComAnnouncement, HttpServletRequest req) {
        try {
            String appId = JwtIOTAuthUtil.getTokenByRequest(req);
            String reaAppId = "";
            if(ObjectUtil.isEmpty(appId)){
                return Result.error(10001,"无效token");
            }else{
                this.updateById(smComAnnouncement);
            }

        } catch (Exception e) {
            return Result.error(10001,e.getMessage());
        }
        return Result.ok(0,"操作成功");
    }

    @Override
    public List<SmzhScreenMsg> queryNoticeByBuildNo(String buildNo) {

        return smzhScreenMsgMapper.searchNoticeByBuildNo(buildNo);
    }
}
